import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';

import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/base_entity.dart';
import 'package:flutter_ncsjqz_getx/model/friend_info_entity.dart';
import 'package:flutter_ncsjqz_getx/model/my_relese_entity.dart';
import 'package:flutter_ncsjqz_getx/model/query_user_detais_entity.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/rong_base_logic.dart';
import 'package:flutter_ncsjqz_getx/util/get_user_util.dart';

class ChatSettingLogic extends RongBaseLogic {
  // 关注 false没关注 true已关注
  bool isAttention = false;

  // 免打扰  false关闭  true开启
  bool disturb = false;

  FriendInfoData? friendInfoData = FriendInfoData()..isFriend = false ;

  QueryUserDetaisData? queryUserDetaisData = QueryUserDetaisData();

  List<MyReleseDataList> myReleseDataList = [];

  String? ryUserId;

  int? friendId;

  TextEditingController textEditingControllerName = TextEditingController();

  setFollow(value) async{
    if(isAttention){ // 取消关注
      BaseEntity baseEntity = await DioService<BaseEntity>().cancelFollow({
        "beFollowUserId": queryUserDetaisData!.userId,
      });
      if(baseEntity.code == 0){
        isAttention = !isAttention;
        update();
      }
    }else{ // 关注
      BaseEntity baseEntity = await DioService<BaseEntity>().clickFollow({
        "beFollowUserId": queryUserDetaisData!.userId,
      });
      if(baseEntity.code == 0){
        isAttention = !isAttention;
        update();
      }
    }
  }

  /// 添加好友
  friendApply() async{
    var data = {
      "friendId":queryUserDetaisData!.userId,
      "friendUserId": queryUserDetaisData!.userId,
      "isInitiator": true,
      "friendType": 0,
      "userId": getUserUtil().userId,
    };
    BaseEntity baseEntity = await DioService<BaseEntity>().friendApply(data);
    BotToast.showText(text: baseEntity.code == 0?"已成功发送好友申请":"已成功发送好友申请",align: Alignment(0, -0.8),contentColor: Colors.black);
    update();
  }

  /// 获取好友信息
  getFriendInfo() async{
    FriendInfoEntity friendInfoEntity = await DioService<FriendInfoEntity>().queryFriendInfo (friendId);
    QueryUserDetaisEntity queryUserDetaisEntity  = await DioService<QueryUserDetaisEntity>().queryUserDetail (friendId!,'');
    if(friendInfoEntity.code == 0){
      friendInfoData = friendInfoEntity.data;
      if(friendInfoData!.postList != null){
        friendInfoData!.postList!.forEach((element) {
          MyReleseDataList my = MyReleseDataList();
          my.postUrls = element.postUrls;
          my.isAttention = element.isAttention;
          my.comments = element.comments;
          my.postTitle = element.postTitle;
          my.postContent = element.postContent;
          my.releaseTime = element.releaseTime;
          my.postId = element.postId;
          my.userId = element.userId;
          myReleseDataList.add(my);
        });
      }
    }
    if(queryUserDetaisEntity.code == 0) {
      queryUserDetaisData = queryUserDetaisEntity.data;
      disturb = queryUserDetaisData!.isDisturb!;
      isAttention = queryUserDetaisData!.isAttention!;
      textEditingControllerName.text = queryUserDetaisData!.remark!;
    }
    update();
  }

  /// 设置昵称和免打扰
  setRemark(targetId,{Disturb}) async{
    /**
      {
        "friendId": 0,  targetId
        "isDisturb": true, 是否免打扰
        "isFollow": true,//是否关注
        "remarkName": "string",//备注名称
        }
     */
    Map<String,dynamic> map=Map();
    map['friendId']=targetId;
    map['isDisturb']=Disturb;
    map['isFollow']=isAttention;
    // 输入为空时  userName设置为备注
    map['remarkName']= textEditingControllerName.text == "" ?friendInfoData!.userName:textEditingControllerName.text;
    BaseEntity baseEntity = await DioService<BaseEntity>().updateRemark(map);
    if(baseEntity.code == 0){
      setConversationNotificationStatus(1, ryUserId, Disturb);
      disturb = Disturb;
      update();
    }
  }
}
